package com.yeyaomai.dksns.flowis.quartz;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
//import org.springframework.data.mongodb.core.mapreduce.GroupByResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import com.yeyaomai.dksns.flowis.dao.DetailDao;
import com.yeyaomai.dksns.flowis.dao.MongoDBBasicDao;
import com.yeyaomai.dksns.flowis.dao.TrendAnalysisDayDao;
import com.yeyaomai.dksns.flowis.domain.TrendAnalysisDay;
import com.yeyaomai.dksns.flowis.domain.flowisdetail.FlowisStatDetailDTO;
import com.yeyaomai.dksns.flowis.util.CollectionNames;
import com.yeyaomai.dksns.flowis.util.VelocityToolDateUtils;

//第二天执行第一天定时任务
//
public class TrendAnalysisDayExecutor extends MongoDBBasicDao<TrendAnalysisDay> {

	@Resource
	private TrendAnalysisDayDao trendAnalysisDayDao;

	@Resource
	private DetailDao detailDao;

	final Logger logger = Logger.getLogger("quartz");

	public long countTrendAnalysisDayByTime(String fromTrendDay,
			String toTrendDay) {
		Query query = new Query(Criteria.where("access_date").gte(fromTrendDay)
				.lt(toTrendDay));

		long pvcount = mongoTemplate.count(query, FlowisStatDetailDTO.class);
		return pvcount;
	}

	public void saveTrendAnalysisByDay() throws ParseException {
		String[] dates = VelocityToolDateUtils.getDateRange(new Date(), 1)
				.split(",");
		String fromTrendDay = dates[0];
		String toTrendDay = dates[1];

		try {
			int pageview = this.getPageViewByTime(fromTrendDay, toTrendDay);
			int uv = this.getUvByTime(fromTrendDay, toTrendDay);
			int ip = this.getIpByTime(fromTrendDay, toTrendDay);
			int newuv = this.getNewUvByTime(fromTrendDay, toTrendDay);
			SimpleDateFormat formats = new SimpleDateFormat("yyyy-MM-dd");

			TrendAnalysisDay trendanalysisDay = new TrendAnalysisDay();
			trendanalysisDay.setTrendDay(formats.parse(fromTrendDay));
			trendanalysisDay.setPageView(pageview);
			trendanalysisDay.setUniqueHost(uv);
			trendanalysisDay.setIp(ip);
			trendanalysisDay.setNewUniqueHost(newuv);

			trendAnalysisDayDao.addTrendAnalysisDay(trendanalysisDay);
			logger.info("###########按天统计执行完成，写入数据################::saveTrendAnalysisByDay()"+new Date());
		} catch (Exception e) {
			logger.info("###########按天统计执行失败，saveTrendAnalysisByDay()############"+new Date());
			e.printStackTrace();
		}

	
	}

	/**
	 * 定时任务处理PV
	 */
	private int getPageViewByTime(String fromTrendDay, String toTrendDay) {

		int pvcount = (int) this.countTrendAnalysisDayByTime(fromTrendDay,
				toTrendDay);
		return pvcount;
	}

	/**
	 * 定时任务处理UV
	 */
	private int getUvByTime(String fromTrendDay, String toTrendDay) {
		String mapFunction = "{ cookieId: 0}";
		String reduceFunction = "function(doc, prev) {prev.cookieId+=1}";
		Criteria criteria = Criteria.where("access_date").gte(fromTrendDay)
				.lte(toTrendDay);
		int uvcount = detailDao.group(criteria, CollectionNames.COOKIEID,
				mapFunction, reduceFunction, FlowisStatDetailDTO.class)
				.getKeys();
		return uvcount;
	}

	/**
	 * 定时任务处理IP
	 */
	private int getIpByTime(String fromTrendDay, String toTrendDay) {
		String mapFunction = "{ip: 0}";
		String reduceFunction = "function(doc, prev) {prev.ip+=1}";
		Criteria criteria = Criteria.where("access_date").gte(fromTrendDay)
				.lte(toTrendDay);
		int ipcount = detailDao.group(criteria, CollectionNames.IP,
				mapFunction, reduceFunction, FlowisStatDetailDTO.class)
				.getKeys();
		return ipcount;
	}

	/**
	 * 定时任务处理newuv
	 */
	private int getNewUvByTime(String fromTrendDay, String toTrendDay) {
		int newuvcount = 0;
		String mapFunction = "{cookieId: 0}";
		String reduceFunction = "function(doc, prev) {prev.cookieId+=1}";
		Criteria criteria = Criteria.where("access_date").gte(fromTrendDay)
				.lte(toTrendDay).and("visitCount").is(1);
		newuvcount = detailDao.group(criteria,
				CollectionNames.COOKIEID, mapFunction, reduceFunction,
				FlowisStatDetailDTO.class).getKeys();
		/*for (FlowisStatDetailDTO engine : result) {
			if (engine.getVisitCount().equals(1)) {
				newuvcount = Integer
						.valueOf(engine.getCookieId().split("\\.")[0]);
			}
		}*/
		return newuvcount;
	}
}
